package com.drug.service.impl;

import com.drug.domain.DirgRg;
import com.drug.domain.DrugInformation;
import com.drug.domain.Result;
import com.drug.domain.ReturnGoods;
import com.drug.mapper.DirgRgMapper;
import com.drug.mapper.DrugInformationMapper;
import com.drug.mapper.HospitalMapper;
import com.drug.mapper.ReturnGoodsMapper;
import com.drug.service.SalesReturnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SalesReturnServiceImpl implements SalesReturnService {

    @Autowired
    private ReturnGoodsMapper returnGoodsMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private DirgRgMapper dirgRgMapper;




    /**
     * 查询退货单
     * @param map
     * @param
     * @return
     */
    @Override
    public Result findtuihuodan(Map map, Integer page) {
        PageHelper.startPage(page,5);
        String dinumber = (String)map.get("dinumber");
        if (dinumber==null || dinumber==""){
            dinumber=null;
        }
        String cname = (String)map.get("cname");
        if (cname==null || cname==""){
            cname=null;
        }
        String cengci = (String)map.get("cengci");
        if (cengci==null || cengci=="" ){
            cengci=null;
        }
        String rightPrice = (String)map.get("rightPrice");
        if (rightPrice==null || rightPrice=="" ){
            rightPrice=null;
        }
        String hospital = (String)map.get("hospital");
        if (hospital==null || hospital=="" ){
            hospital=null;
        }
        //根据医院名字查id
        Integer id = hospitalMapper.selectByName(hospital);
        ReturnGoods returnGoods = new ReturnGoods();
        returnGoods.setRgNumber(dinumber);
        returnGoods.setRgName(cname);
        returnGoods.setRgState(cengci);
        returnGoods.setRgUId(id);
        List<ReturnGoods> list = returnGoodsMapper.selectall(returnGoods);
        PageInfo<ReturnGoods> pageInfo = new PageInfo<>(list);
        Result result = new Result<>();
        result.setData(pageInfo);
        return result;
    }

    /**
     * 添加退货单
     * @param map
     * @return
     */
    @Override
    public Result addtuihuodan(Map map) {

        String hName =(String) map.get("hName");

        //根据医院名字查id
        Integer id = hospitalMapper.selectByName(hName);
        String poContacts =(String) map.get("poContacts");
        String poName = (String)map.get("poName");
        String poPhone = (String)map.get("poPhone");
        String poRemarks =(String) map.get("poRemarks");
        String poSupplyTime =(String) map.get("poSupplyTime");
        ReturnGoods returnGoods = new ReturnGoods();
        returnGoods.setRgUId(id);
        returnGoods.setRgContacts(poContacts);
        returnGoods.setRgName(poName);
        returnGoods.setRgPhone(poPhone);
        returnGoods.setRgRemarks(poRemarks);
       // returnGoods.setRgReturnTime((poSupplyTime));
        returnGoodsMapper.addtuihuodan(returnGoods);
        Result result = new Result<>();
        result.setMsg("yes");
        return result;
    }

    /**
     *查询退货单和商品中间表
     * @param map
     * @return
     */
    @Override
    public Result findAll(Map map,Integer page) {
        PageHelper.startPage(page,5);
        String dinumber = (String) map.get("dinumber");
        if (dinumber==null || dinumber=="" ){
            dinumber=null;
        }
        String cname = (String) map.get("cname");
        if (cname==null || cname=="" ){
            cname=null;
        }
        String jixing = (String) map.get("jixing");
        if (jixing==null || jixing=="" ){
            jixing=null;
        }
        String guige = (String) map.get("guige");
        if (guige==null || guige=="" ){
            guige=null;
        }
        String danwei = (String) map.get("danwei");
        if (danwei==null || danwei=="" ){
            danwei=null;
        }
        String id = (String) map.get("id");
        DirgRg dirgRg = new DirgRg();
        if (id==null || id == ""){
            id=null;
        }else {
            dirgRg.setDirgRgid(Integer.parseInt(id));
        }
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setDiNumer(dinumber);
        drugInformation.setDiName(cname);
        drugInformation.setDiForm(jixing);
        drugInformation.setDiSpecifications(guige);
        drugInformation.setDiUnit(danwei);
        drugInformation.setDirgRg(dirgRg);

        List<DrugInformation> selecttuihuo = drugInformationMapper.selecttuihuo(drugInformation);
        PageInfo<DrugInformation> pageInfo = new PageInfo<>(selecttuihuo);
        Result result = new Result<>();
        result.setData(pageInfo);
        return result;
    }

    /**
     * 添加退货量
     * @param
     * @return
     */
    @Override
    public Result addthl(Integer[] diId,Integer[] tuihuoliang,String[] tuihuoyuanyin,Integer zhongjianbiao) {

        //TODO
        DirgRg dirgRg = new DirgRg();
        for (int i = 0; i < diId.length; i++) {
            dirgRg.setDirgRgid(zhongjianbiao);
            dirgRg.setDirgDiid(diId[i]);
            dirgRg.setDirgNumber(tuihuoliang[i]);
            dirgRg.setDirgYuanyin(tuihuoyuanyin[i]);
            int j = dirgRgMapper.addthl(dirgRg);

        }
        Result result = new Result<>();
        result.setMsg("yes");
        return result;
    }

    @Override
    public Result delectthd(Integer id) {
        ReturnGoods returnGoods = new ReturnGoods();
        returnGoods.setRgId(id);
        DirgRg dirgRg = new DirgRg();
        dirgRg.setDirgRgid(id);
        returnGoodsMapper.delectthd(returnGoods);
        dirgRgMapper.delectthd(dirgRg);
        Result result = new Result<>();
        result.setMsg("i");
        return result;
    }
}
